package y24.m4;

import java.util.Arrays;
import java.util.Random;

public class Test1 {
    public static void main(String[] args) {
        int numSimulations = 100000000;  // 模拟购买的彩票次数
        int numTickets = 5;  // 选中的号码数量
        int numsRange = 35;  // 号码范围
        int additionalNumRange = 12;  // 附加号码的范围

        // 设定一个开奖的号码组合
        Random random = new Random();
        int[] winningNums = new int[numTickets];
        int[] winningAdditionalNums = new int[2];

        for (int i = 0; i < numTickets; i++) {
            winningNums[i] = random.nextInt(numsRange) + 1;
        }

        for (int i = 0; i < 2; i++) {
            winningAdditionalNums[i] = random.nextInt(additionalNumRange) + 1;
        }

        // 模拟购买彩票并统计各奖项中奖的次数
        int[] numPrizes = new int[6];  // 每个奖项的中奖次数

        for (int s = 0; s < numSimulations; s++) {
            // 随机选取5个号码和2个附加号码
            int[] selectedNums = new int[numTickets];
            for (int i = 0; i < numTickets; i++) {
                selectedNums[i] = random.nextInt(numsRange) + 1;
            }

            int[] selectedAdditionalNums = new int[2];
            for (int i = 0; i < 2; i++) {
                selectedAdditionalNums[i] = random.nextInt(additionalNumRange) + 1;
            }

            // 判断中奖情况
            if (Arrays.equals(selectedNums, winningNums) && Arrays.equals(selectedAdditionalNums, winningAdditionalNums)) {
                numPrizes[1]++;
            } else if (Arrays.equals(selectedNums, winningNums) && !Arrays.equals(selectedAdditionalNums, winningAdditionalNums)) {
                numPrizes[2]++;
            } else if (Arrays.equals(selectedAdditionalNums, winningAdditionalNums) && Arrays.stream(selectedNums).filter(value -> Arrays.stream(winningNums).anyMatch(x -> x == value)).count() == 4) {
                numPrizes[3]++;
            } else if (Arrays.equals(selectedAdditionalNums, winningAdditionalNums) && Arrays.stream(selectedNums).filter(value -> Arrays.stream(winningNums).anyMatch(x -> x == value)).count() == 3) {
                numPrizes[4]++;
            } else if (Arrays.stream(selectedNums).filter(value -> Arrays.stream(winningNums).anyMatch(x -> x == value)).count() == 4) {
                numPrizes[5]++;
            }
        }

        // 计算各奖项的概率
        double[] probabilityEstimates = new double[6];
        for (int i = 1; i <= 5; i++) {
            probabilityEstimates[i] = (double) numPrizes[i] / numSimulations;
            System.out.println("中" + i + "等奖的概率为：" + probabilityEstimates[i]);
        }
    }
}
